﻿using MediatR;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using wt.wine.Application.Common.Interfaces;
using wt.wine.Application.Common.Models;

namespace wt.wine.Application.ManageDevicerecordtalbe.Queries
{
    /// <summary>
    /// 获取设备加酒记录
    /// </summary>
    public class GetDevicerecordListQueries : IRequest<Json<PaginatedList<GetDevicerecordListDTO>>>
    {
        /// <summary>
        /// 开始时间
        /// </summary>
        public string startTime { get; set; }
        /// <summary>
        /// 截止时间
        /// </summary>
        public string endTime { get; set; }
        /// <summary>
        /// 商品id
        /// </summary>
        public string skuname { get; set; }
        /// <summary>
        /// 第几页
        /// </summary>
        public int pageIndex { get; set; }
        /// <summary>
        /// 页码
        /// </summary>
        public int pageSize { get; set; }
    }

    public class GetDevicerecordListQueriesHandler : IRequestHandler<GetDevicerecordListQueries, Json<PaginatedList<GetDevicerecordListDTO>>>
    {
        private readonly ICurrentUserService _icurrentUserServices;
        private readonly IDevicerecordRepository _idevicerecordRepository;
        public GetDevicerecordListQueriesHandler(ICurrentUserService icurrentUserServices, IDevicerecordRepository idevicerecordRepository)
        {
            _icurrentUserServices = icurrentUserServices;
            _idevicerecordRepository = idevicerecordRepository;
        }
        public async Task<Json<PaginatedList<GetDevicerecordListDTO>>> Handle(GetDevicerecordListQueries request, CancellationToken cancellationToken)
        {
            DateTime dt_begin;
            DateTime dt_end;
            int userid = _icurrentUserServices.GetUserId();
            if (request.pageIndex <= 0 || request.pageSize > 1000)
            {
                return Json<PaginatedList<GetDevicerecordListDTO>>.Failed("参数不能为空");
            }
            else if (!DateTime.TryParse(request.startTime, out dt_begin))
            {
                return Json<PaginatedList<GetDevicerecordListDTO>>.Failed("开始时间异常");
            }
            else if (!DateTime.TryParse(request.endTime, out dt_end))
            {
                return Json<PaginatedList<GetDevicerecordListDTO>>.Failed("截止时间异常");
            }
            else
            {
                var model = await _idevicerecordRepository.GetDevicerecoreList(userid, request.skuname, DateTime.Parse(dt_begin.ToString("yyyy-MM-dd 00:00:00")), DateTime.Parse(dt_end.ToString("yyyy-MM-dd 23:59:59")), request.pageIndex, request.pageSize, cancellationToken);
                return Json<PaginatedList<GetDevicerecordListDTO>>.Success("查询成功", model);
            }
        }
    }
}
